package com.haiheng.module_main.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.AdapterView.OnItemClickListener
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.haiheng.common.constant.Keys
import com.haiheng.common.constant.RourePage
import com.haiheng.common.fragment.RouterFragment
import com.haiheng.library.log.LogUtils
import com.haiheng.library_base.adapter.BaseAdapter
import com.haiheng.library_base.model.ApplicationViewModel
import com.haiheng.library_base.model.BaseViewModel
import com.haiheng.library_base.msg.MsgEvent
import com.haiheng.module_main.R
import com.haiheng.module_main.adapter.HomeListAdapter
import com.haiheng.module_main.bean.HomeIconBean
import com.haiheng.module_main.databinding.MainFragmentBinding


/**
 *
 * @Des:
 * @Author: hwz
 * @Date:2022/4/18 0018
 * @Version: 1.0
 */
class MainFragment : RouterFragment(){

    private lateinit var binding: MainFragmentBinding
    private val homeListAdapter = HomeListAdapter()


    private val observer = Observer<MsgEvent> { msgEvent ->
        onMessageChange(msgEvent)
    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        binding = MainFragmentBinding.inflate(inflater, container, false)
        return binding!!.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

    }


    override fun onPause() {
        super.onPause()
        clearMsg()
    }

    private fun onMessageChange(msgEvent: MsgEvent?) {
    }

    private fun observEven() {
        if (ApplicationViewModel.get().even != null) {
            if (ApplicationViewModel.get().even.hasObservers()) {
                ApplicationViewModel.get().even.removeObserver(observer)
            }
            ApplicationViewModel.get().even.observe(this,observer)
        }
    }
    fun clearMsg(){
        if (ApplicationViewModel.get().even != null) {
            if (ApplicationViewModel.get().even.hasObservers()) {
                ApplicationViewModel.get().even.removeObserver(observer)
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        LogUtils.d("onDestroyView")


    }

    override fun initView() {
        binding.includeTile.tvTitle.setText(getString(R.string.text_home_title))
        binding.includeTile.ivSetting.setOnClickListener {
            activity.navigation(RourePage.SETTING_PAGE)
        }
        binding?.rvHome?.layoutManager = GridLayoutManager(activity,4)
        binding?.rvHome?.adapter = homeListAdapter
        var list  =  ArrayList<HomeIconBean>()
        /**
         * "单册上架" -> Single Volume Deployment
        "智慧上架" ->Smart Deployment
        "图书下架" ->Book Deployment
        "图书倒架" ->Book Off- shelving
        "图书理架" ->Book Re- shelving
        "查找定位" ->Find and Locate
        "图书剔旧" ->Book Retirement
        "图书盘点" ->Book Inventory
        "资产盘点" ->Asset Inventory
        "数据同步" ->Data Synchronization
        "报警图书" ->Alarmed Books
        "错架整理" ->Mis-shelved Correction
         */
        list.add(HomeIconBean("单册上架",R.mipmap.ic_home_dancheshangjia))
        list.add(HomeIconBean("智慧上架",R.mipmap.ic_home_zhihui_shangjia))
        list.add(HomeIconBean("图书下架",R.mipmap.ic_home_xiajia))
        list.add(HomeIconBean("图书倒架",R.mipmap.ic_home_daojia))
        list.add(HomeIconBean("图书理架",R.mipmap.ic_home_lijia))
        list.add(HomeIconBean("查找定位",R.mipmap.ic_home_chazhao))
        list.add(HomeIconBean("图书剔旧",R.mipmap.ic_home_tijiiu))
        list.add(HomeIconBean("图书盘点",R.mipmap.ic_home_tushupandian))
        list.add(HomeIconBean("资产盘点",R.mipmap.ic_home_zichanpandian))
        list.add(HomeIconBean("报警图书",R.mipmap.ic_home_baojing))
        list.add(HomeIconBean("数据同步",R.mipmap.ic_home_tongbun))
//        list.add(HomeIconBean("错架整理",R.mipmap.ic_home_lijia))
        homeListAdapter.setNewData(list)
        homeListAdapter.setOnItemClickListener(object : BaseAdapter.OnItemClickListener {
            override fun onItemClick(holder: BaseAdapter.ViewBindHolder, position: Int) {
                var item = homeListAdapter.getItem(position)
                when(item.name){
                    "单册上架" ->{
                        activity.navigation(RourePage.SINGLE_VOLUME_DEPLOYMENT)
                    }
                    "智慧上架" ->{
                        activity.navigation(RourePage.SMART_DEPLOYMENT)
                    }
                    "图书下架" ->{
                        activity.navigation(RourePage.BOOK_WITHDRAWAL)
                    }
                    "图书倒架" ->{
                        activity.navigation(RourePage.BOOK_OFF_SHELVING)
                    }
                    "图书理架" ->{
                        activity.navigation(RourePage.BOOK_RE_SHELVING)
                    }
                    "查找定位" ->{
                        activity.navigation(RourePage.FIND_AND_LOCATE)
                    }
                    "图书剔旧" ->{
                        activity.navigation(RourePage.BOOK_RETIREMENT)
                    }
                    "图书盘点" ->{
                        activity.navigation(RourePage.BOOK_INVENTORY, bundleOf(Keys.PAGE_TYPE to RourePage.BOOK_INVENTORY))
                    }
                    "资产盘点" ->{
                        activity.navigation(RourePage.ASSET_INVENTORY)
                    }
                    "数据同步" ->{
                        activity.navigation(RourePage.DATA_SYNCHRONIZATION)
                    }
                    "报警图书" ->{
                        activity.navigation(RourePage.ALARMED_BOOKS)
                    }
                    "错架整理" ->{
                        activity.navigation(RourePage.MIS_SHELVED_CORRECTION)
                    }
                }
            }

        })
    }

    override fun initViewModel(): BaseViewModel? {

        return null
    }

    override fun initLoad() {
    }


    override fun onResume() {
        super.onResume()
        LogUtils.e("onResume")
        observEven()

    }



    private fun initListener() {

    }



}